<template>
  <div class="home">
    <div class="content">
      <div class="header">
        <el-page-header content="编辑页面" @back="goBack"></el-page-header>
      </div>
      <div class="selectMoudle">
        <div>
          <span class="text">模块</span>
          <el-radio-group v-model="editorData.radio1" fill="#1d2f70">
            <el-radio-button
              v-for="(item, index) in radio"
              :label="item.label"
              :key="index"
            ></el-radio-button>
          </el-radio-group>
        </div>
        <div>
          <el-button class="sendBtn" @click="send">
            发布
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </div>
      </div>

      <div class="inputBox">
        <input
          type="text"
          placeholder="输入标题"
          v-model="editorData.title"
          @change="onChangeEdit"
        />
      </div>

      <div class="inputBox">
        <el-upload
          class="upload-demo"
          action="https://jifen.zhportinfo.com/api/v1/resorcefj"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :multiple="false"
          :limit="1"
          :on-exceed="handleExceed"
          :file-list="editorData.fileList"
          :http-request="upload"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">附件上传</div>
        </el-upload>
      </div>
      <div class="selectZiyuan">
        <span class="resourceTitle">资源模块</span>
        <el-select
          v-model="editorData.classify"
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="(item, index) in options"
            :key="index"
            :label="item.title"
            :value="item.index"
          ></el-option>
        </el-select>
      </div>
      <div class="editor">
        <textEditor
          @EditorChange="editorChange"
          :oldcontent="editorData.html"
          :oldremoveIds="editorData.deleImgId"
        ></textEditor>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "axios";
import textEditor from "@/components/quillEditor";
import { get_resourcelabel } from "@/api/resourcelabel";
// import { textReplace } from "../../common/textReplace";
import { post_draft } from "@/api/draft";
import { uploadResorcefj } from "@/api/resorcefj";
export default {
  name: "Editor",
  data() {
    return {
      editorData: {
        radio1: "资源共享",
        title: "",
        titleImageUrl: "",
        html: "",
        deleImgId: [],
        textReplace: "",
        deletTitleImageUrl: [],
        userId: this.$store.getters.userInfo._id,
        fileList: [],
        fileDelectArr: [],
        classify: "",
      },
      radio: [
        {
          name: "资源共享",
          label: "资源共享",
        },
      ],
      options: [],
      // timer: null
    };
  },
  async created() {
    let resourcelabelData = await get_resourcelabel();
    this.options = resourcelabelData.data;
  },
  components: {
    textEditor,
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    editorChange(data) {
      this.editorData.html = data.html;
      this.editorData.deleImgId = data.removeIds;
      // this.editorData.textReplace = textReplace(data.html);
      console.log(this.editorData);
      this.onChangeEdit();
    },
    async onChangeEdit() {
      let editView = await post_draft(this.editorData);
      console.log("editView", editView);
      let edit_id = editView.data._id;
      this.$router.replace({
        path: "/editResources",
        query: {
          _id: edit_id,
        },
      });
    },
    send() {
      // if (this.editorData.articleimg == "") {
      //   return this.$alert("请添加题图", "提示", {
      //     confirmButtonText: "确定",
      //     callback: () => {}
      //   });
      // }
      if (this.editorData.title == "") {
        return this.$alert("请完善文章标题", "提示", {
          confirmButtonText: "确定",
          callback: () => {},
        });
      }
      if (this.editorData.html == "") {
        return this.$alert("请完善文章内容", "提示", {
          confirmButtonText: "确定",
          callback: () => {},
        });
      }
    },
    //上传文件
    //文件列表移除文件时的钩子
    handleRemove(file, fileList) {
      console.log("handleRemove", file, fileList);
    },
    //点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
    },
    //文件超出个数限制时的钩子
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    //删除文件之前的钩子，参数为上传的文件和文件列表，若返回 false 或者返回 Promise 且被 reject，则停止删除。
    beforeRemove(file, fileList) {
      console.log(fileList);

      return this.$confirm(`确定移除 ${file.name}？`);
    },
    upload(fileObj) {
      console.log(fileObj.file.name);
      var fileName = fileObj.file.name;
      var index1 = fileName.lastIndexOf(".");
      var index2 = fileName.length;
      let lic = fileName.substring(index1 + 1, index2);
      console.log(lic);

      var formData = new FormData();
      formData.append("lication", lic);
      formData.append("file", fileObj.file);
      uploadResorcefj(formData)
        .then((response) => {
          console.log(response);
          this.editorData.fileList.push(response);
          console.log("总列表", this.editorData.fileList);
          this.onChangeEdit();
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  background: #fff;
}
.content {
  width: 100%;
  .header {
    margin: 0 auto;
    width: 1200px;
    margin-top: 20px;
  }
  .selectMoudle {
    margin: 0 auto;
    width: 1200px;
    margin-top: 40px;
    display: flex;
    justify-content: space-between;
    .text {
      font-size: 20px;
      font-weight: 700;
      color: #757575;
      margin-right: 20px;
    }
    .sendBtn {
      background: #1d2f70;
      border: #1d2f70;
      color: #fff;
      &:hover {
        background: rgb(53, 89, 180);
      }
    }
  }
  .selectZiyuan {
    margin: 0 auto;
    width: 1200px;
    margin-top: 30px;
    .resourceTitle {
      font-size: 20px;
      color: #6c6c6c;
      font-weight: 600;
      margin-right: 20px;
    }
  }
  .inputBox {
    margin: 0 auto;
    width: 1200px;
    margin-top: 30px;
    input {
      outline-style: none;
      outline-style: none;
      border: 0px;
      border-bottom: 2px solid #ccc;
      width: 1200px;
      padding: 13px 14px;
      font-size: 28px;
      font-weight: 700;
      font-family: "Microsoft soft";
      box-sizing: border-box;
    }
  }
  // margin-top: -19px;
  padding: 20px;
  box-sizing: border-box;
  .editor {
    min-height: 500px;
    margin-top: 40px;
  }
}
</style>
